from django.shortcuts import render

# Create your views here.
from drf_haystack.viewsets import HaystackViewSet
from rest_framework.generics import ListAPIView
from .models import SKU
from .serializers import SKUSerializer, SKUSearchSerializer
from rest_framework.filters import OrderingFilter

class SKUListView(ListAPIView):
    """商品列表数据查询"""
    # filter_backends = [OrderingFilter]
    # ordering_fields = ['create_time', 'price', 'sales']
    serializer_class = SKUSerializer
    # 指定过滤器:需要指定排序后端
    filter_backends = (OrderingFilter,)
    # 指定排序字段：搭配filter_backends使用的
    ordering_fields = ('create_time', 'price', 'sales')

    def get_queryset(self):
        category_id=self.kwargs.get('category_id')
        return SKU.objects.filter(is_launched=True, category_id=category_id)

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU] #指定查询集
    serializer_class = SKUSearchSerializer #指定序列化器